﻿#include "ChatOptionDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../Settings.h"
#include "../../MirSounds/SoundManager.h"
#include "../GameScene.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirSounds;

namespace Client::MirScenes::Dialogs
{

    ChatOptionDialog::ChatOptionDialog()
    {
        setIndex(466);
        setLibrary(Libraries::Title);
        Size tempVar(224, 180);
        setSize(&tempVar);
        setMovable(true);
        setSort(true);
        Point tempVar2((Settings::ScreenWidth - getSize()->Width) / 2, (Settings::ScreenHeight - getSize()->Height) / 2);
        setLocation(&tempVar2);

        BeforeDraw += ChatFilterPanel_BeforeDraw;

        FilterTabButton = new MirButton();
        FilterTabButton->setIndex(463);
        FilterTabButton->setPressedIndex(462);
        FilterTabButton->setParent(this);
        FilterTabButton->setLibrary(Libraries::Title);
        FilterTabButton->setSound(SoundList::ButtonA);
        Point tempVar3(8, 8);
        FilterTabButton->setLocation(&tempVar3);
        FilterTabButton->Click += [&] (o, e)
        {
            SwitchTab(0);
        };

        ChatTabButton = new MirButton();
        ChatTabButton->setIndex(464);
        ChatTabButton->setPressedIndex(465);
        ChatTabButton->setParent(this);
        ChatTabButton->setLibrary(Libraries::Title);
        ChatTabButton->setSound(SoundList::ButtonA);
        Point tempVar4(78, 8);
        ChatTabButton->setLocation(&tempVar4);
        ChatTabButton->Click += [&] (o, e)
        {
            SwitchTab(1);
        };

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar5(198, 3);
        CloseButton->setLocation(&tempVar5);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };

        #pragma region Filters

        AllButton = new MirButton();
        AllButton->setIndex(2087);
        AllButton->setLibrary(Libraries::Prguse);
        AllButton->setParent(this);
        Point tempVar6(74, 47);
        AllButton->setLocation(&tempVar6);
        AllButton->setSound(SoundList::ButtonA);
        Size tempVar7(16, 12);
        AllButton->setSize(&tempVar7);
        AllButton->Click += [&] (o, e)
        {
            ToggleAllFilters();
        };

        GeneralButton = new MirButton();
        GeneralButton->setIndex(2071);
        GeneralButton->setLibrary(Libraries::Prguse);
        GeneralButton->setParent(this);
        Point tempVar8(40, 69);
        GeneralButton->setLocation(&tempVar8);
        GeneralButton->setSound(SoundList::ButtonA);
        GeneralButton->Click += [&] (o, e)
        {
            Settings::FilterNormalChat = !Settings::FilterNormalChat;
            CheckAllFilters();
        };

        WhisperButton = new MirButton();
        WhisperButton->setIndex(2075);
        WhisperButton->setLibrary(Libraries::Prguse);
        WhisperButton->setParent(this);
        Point tempVar9(40, 92);
        WhisperButton->setLocation(&tempVar9);
        WhisperButton->setSound(SoundList::ButtonA);
        WhisperButton->Click += [&] (o, e)
        {
            Settings::FilterWhisperChat = !Settings::FilterWhisperChat;
            CheckAllFilters();
        };

        ShoutButton = new MirButton();
        ShoutButton->setIndex(2073);
        ShoutButton->setLibrary(Libraries::Prguse);
        ShoutButton->setParent(this);
        Point tempVar10(40, 115);
        ShoutButton->setLocation(&tempVar10);
        ShoutButton->setSound(SoundList::ButtonA);
        ShoutButton->Click += [&] (o, e)
        {
            Settings::FilterShoutChat = !Settings::FilterShoutChat;
            CheckAllFilters();
        };

        SystemButton = new MirButton();
        SystemButton->setIndex(2085);
        SystemButton->setLibrary(Libraries::Prguse);
        SystemButton->setParent(this);
        Point tempVar11(40, 138);
        SystemButton->setLocation(&tempVar11);
        SystemButton->setSound(SoundList::ButtonA);
        SystemButton->Click += [&] (o, e)
        {
            Settings::FilterSystemChat = !Settings::FilterSystemChat;
            CheckAllFilters();
        };

        LoverButton = new MirButton();
        LoverButton->setIndex(2077);
        LoverButton->setLibrary(Libraries::Prguse);
        LoverButton->setParent(this);
        Point tempVar12(135, 69);
        LoverButton->setLocation(&tempVar12);
        LoverButton->setSound(SoundList::ButtonA);
        LoverButton->Click += [&] (o, e)
        {
            Settings::FilterLoverChat = !Settings::FilterLoverChat;
            CheckAllFilters();
        };

        MentorButton = new MirButton();
        MentorButton->setIndex(2079);
        MentorButton->setLibrary(Libraries::Prguse);
        MentorButton->setParent(this);
        Point tempVar13(135, 92);
        MentorButton->setLocation(&tempVar13);
        MentorButton->setSound(SoundList::ButtonA);
        MentorButton->Click += [&] (o, e)
        {
            Settings::FilterMentorChat = !Settings::FilterMentorChat;
            CheckAllFilters();
        };

        GroupButton = new MirButton();
        GroupButton->setIndex(2081);
        GroupButton->setLibrary(Libraries::Prguse);
        GroupButton->setParent(this);
        Point tempVar14(135, 115);
        GroupButton->setLocation(&tempVar14);
        GroupButton->setSound(SoundList::ButtonA);
        GroupButton->Click += [&] (o, e)
        {
            Settings::FilterGroupChat = !Settings::FilterGroupChat;
            CheckAllFilters();
        };

        GuildButton = new MirButton();
        GuildButton->setIndex(2083);
        GuildButton->setLibrary(Libraries::Prguse);
        GuildButton->setParent(this);
        Point tempVar15(135, 138);
        GuildButton->setLocation(&tempVar15);
        GuildButton->setSound(SoundList::ButtonA);
        GuildButton->Click += [&] (o, e)
        {
            Settings::FilterGuildChat = !Settings::FilterGuildChat;
            CheckAllFilters();
        };

        #pragma endregion

        #pragma region Transparency

        TransparencyOffButton = new MirButton();
        TransparencyOffButton->setIndex(471);
        TransparencyOffButton->setHoverIndex(472);
        TransparencyOffButton->setPressedIndex(470);
        Point tempVar16(45, 90);
        TransparencyOffButton->setLocation(&tempVar16);
        TransparencyOffButton->setParent(this);
        TransparencyOffButton->setLibrary(Libraries::Title);
        TransparencyOffButton->setSound(SoundList::ButtonA);
        TransparencyOffButton->setVisible(false);
        TransparencyOffButton->Click += [&] (o, e)
        {
            Settings::TransparentChat = false;
            UpdateTransparency();
        };

        TransparencyOnButton = new MirButton();
        TransparencyOnButton->setIndex(474);
        TransparencyOnButton->setHoverIndex(475);
        TransparencyOnButton->setPressedIndex(473);
        Point tempVar17(115, 90);
        TransparencyOnButton->setLocation(&tempVar17);
        TransparencyOnButton->setParent(this);
        TransparencyOnButton->setLibrary(Libraries::Title);
        TransparencyOnButton->setSound(SoundList::ButtonA);
        TransparencyOnButton->setVisible(false);
        TransparencyOnButton->Click += [&] (o, e)
        {
            Settings::TransparentChat = true;
            UpdateTransparency();
        };

        #pragma endregion

        CheckAllFilters();
        UpdateTransparency();
    }

    void ChatOptionDialog::ChatFilterPanel_BeforeDraw(std::any sender, EventArgs *e)
    {
        AllButton->setIndex(AllFiltersOff ? 2087 : 2086);

        GeneralButton->setIndex(Settings::FilterNormalChat ? 2070 : 2071);
        WhisperButton->setIndex(Settings::FilterWhisperChat ? 2074 : 2075);
        ShoutButton->setIndex(Settings::FilterShoutChat ? 2072 : 2073);
        SystemButton->setIndex(Settings::FilterSystemChat ? 2084 : 2085);
        LoverButton->setIndex(Settings::FilterLoverChat ? 2076 : 2077);
        MentorButton->setIndex(Settings::FilterMentorChat ? 2078 : 2079);
        GroupButton->setIndex(Settings::FilterGroupChat ? 2080 : 2081);
        GuildButton->setIndex(Settings::FilterGuildChat ? 2082 : 2083);
    }

    void ChatOptionDialog::SwitchTab(int tab)
    {
        if (tab == 0)
        {
            FilterTabButton->setIndex(463);
            FilterTabButton->setPressedIndex(462);
            ChatTabButton->setIndex(464);
            ChatTabButton->setPressedIndex(465);
            setIndex(466);

            //Show all buttons on filter tab
            AllButton->setVisible(true);
            GeneralButton->setVisible(true);
            WhisperButton->setVisible(true);
            ShoutButton->setVisible(true);
            SystemButton->setVisible(true);
            LoverButton->setVisible(true);
            MentorButton->setVisible(true);
            GroupButton->setVisible(true);
            GuildButton->setVisible(true);

            //hide all transparency buttons
            TransparencyOffButton->setVisible(false);
            TransparencyOnButton->setVisible(false);
        }
        else if (tab == 1)
        {
            FilterTabButton->setIndex(462);
            FilterTabButton->setPressedIndex(463);
            ChatTabButton->setIndex(465);
            ChatTabButton->setPressedIndex(464);
            setIndex(467);

            //Hide all buttons on filter tab
            AllButton->setVisible(false);
            GeneralButton->setVisible(false);
            WhisperButton->setVisible(false);
            ShoutButton->setVisible(false);
            SystemButton->setVisible(false);
            LoverButton->setVisible(false);
            MentorButton->setVisible(false);
            GroupButton->setVisible(false);
            GuildButton->setVisible(false);

            //show all transparency buttons
            TransparencyOffButton->setVisible(true);
            TransparencyOnButton->setVisible(true);
        }
    }

    void ChatOptionDialog::CheckAllFilters()
    {
        if (!Settings::FilterNormalChat && !Settings::FilterWhisperChat && !Settings::FilterShoutChat && !Settings::FilterSystemChat && !Settings::FilterLoverChat && !Settings::FilterMentorChat && !Settings::FilterGroupChat && !Settings::FilterGuildChat)
        {
            AllFiltersOff = true;
        }
        else
        {
            AllFiltersOff = false;
        }

        GameScene::Scene->ChatDialog->Update();
    }

    void ChatOptionDialog::ToggleAllFilters()
    {
        if (AllFiltersOff)
        {
            //turn all filters on
            Settings::FilterNormalChat = true;
            Settings::FilterWhisperChat = true;
            Settings::FilterShoutChat = true;
            Settings::FilterSystemChat = true;
            Settings::FilterLoverChat = true;
            Settings::FilterMentorChat = true;
            Settings::FilterGroupChat = true;
            Settings::FilterGuildChat = true;

            AllButton->setIndex(2086);
        }
        else
        {
            //turn all filters off
            Settings::FilterNormalChat = false;
            Settings::FilterWhisperChat = false;
            Settings::FilterShoutChat = false;
            Settings::FilterSystemChat = false;
            Settings::FilterLoverChat = false;
            Settings::FilterMentorChat = false;
            Settings::FilterGroupChat = false;
            Settings::FilterGuildChat = false;

            AllButton->setIndex(2087);
        }

        AllFiltersOff = !AllFiltersOff;

        GameScene::Scene->ChatDialog->Update();
    }

    void ChatOptionDialog::UpdateTransparency()
    {
        if (Settings::TransparentChat)
        {
            GameScene::Scene->ChatDialog->setForeColour(Color::FromArgb(190, 15, 0, 0));
            GameScene::Scene->ChatDialog->setBackColour(Color::FromArgb(190, 15, 0, 0));

            TransparencyOnButton->setIndex(474);
            TransparencyOnButton->setHoverIndex(475);

            TransparencyOffButton->setIndex(470);
            TransparencyOffButton->setHoverIndex(470);
        }
        else
        {
            GameScene::Scene->ChatDialog->setForeColour(Color::White);
            GameScene::Scene->ChatDialog->setBackColour(Color::White);

            TransparencyOnButton->setIndex(473);
            TransparencyOnButton->setHoverIndex(473);

            TransparencyOffButton->setIndex(471);
            TransparencyOffButton->setHoverIndex(472);
        }
    }

    void ChatOptionDialog::Toggle()
    {
        if (!getVisible())
        {
            Show();
        }
        else
        {
            Hide();
        }
    }
}
